package ex.inte;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author small瑞
 */
public class MyDateTime implements MyDate {
    private int year ;
    private int month;
    private int day ;
    private int hour ;
    private int minute;
    private int second;
    private int hsecond;

    public MyDateTime(int year, int month, int day, int hour, int minute, int second) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    public MyDateTime() {

    }

    @Override
    public int compareTo(MyDateTime date) throws ParseException {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date data = format.parse(date.getStringTime());
        Date date1 = format.parse(this.getStringTime());
        int result = date1.compareTo(data);
        switch (result){
            case 1 ->System.out.println("前者时间更久远!");
            case 0  ->System.out.println("两者时间一致!");
            case -1  ->System.out.println("后者时间更久远!");
            default ->System.out.println("错误格式!");
        }

        return result ;
    }
    public Date getDate() throws ParseException {
        String pattern = "yyyy-MM-dd HH:mm:ss.sss";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Random random = new Random();
        String time = random.nextInt(3000)
                +"-"
                + (1 + random.nextInt(12))
                +"-"
                + (1 + random.nextInt(30))
                +" "
                + random.nextInt(24)
                +":"
                + random.nextInt(60)
                +":"
                + random.nextInt(60)
                +"."
                +  random.nextInt(1000);
        Date date = format.parse(time);
        return date ;
    }
    public String getStringTime(){
        String time = Integer.toString(this.year)
                +"-"
                +Integer.toString(this.month)
                +"-"
                +Integer.toString(this.day)
                +" "
                +Integer.toString(this.hour)
                +":"
                +Integer.toString(this.minute)
                +":"
                +Integer.toString(this.second);
        return time;
    }
}
